package paper.dao.impl;

import java.util.List;

import keter.dao.base.KeterAbstractDao;
import keter.dao.org.UnitDao;
import keter.domain.Unit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import paper.dao.PaperArchiveDao;
import paper.domain.Paper;
import paper.domain.PaperArchive;

@Repository
public class PaperArchiveDaoImpl extends KeterAbstractDao<PaperArchive> implements
		PaperArchiveDao {

	@Autowired
	UnitDao udao;

	@Override
	public void archive(Paper paper) {
		PaperArchive pa = new PaperArchive();
		// pa.setId(paper.getId());
		pa.setPaperId(paper.getId());
		pa.setAccsoryFileId(paper.getAccessoryFileId());
		pa.setName(paper.getName());
		pa.setCreatorName(paper.getCreator().getName());
		pa.setMasterName(paper.getMaster().getName());
		String collegeCode = Unit
				.getSubCode(paper.getCreator().getAccount().getUnit(), 4);
		pa.setCollegeCode(collegeCode);
		pa.setCollegeName(udao.findByCode(collegeCode).getName());
		String majorCode = Unit.getSubCode(paper.getCreator().getAccount().getUnit(), 6);
		pa.setMajorCode(majorCode);
		pa.setMajorName(udao.findByCode(majorCode).getName());
		String classCode = Unit.getSubCode(paper.getCreator().getAccount().getUnit(), 8);
		pa.setClassCode(classCode);
		pa.setClassName(udao.findByCode(classCode).getName());
		System.out.println("~~~~~~~~~~~~~~~~~~" + saveEntity(pa).getId()
				+ "~~~~~~~~~~~~~~~~~~~~~~");
	}

	/**
	 * <p>
	 * Method ：findByMajorCode
	 * <p>
	 * Description : 查询当前组织机构代码下属的论文
	 * 
	 * @see paper.dao.PaperArchiveDao#findByUnitCode(java.lang.String)
	 * @author gulixing@msn.com
	 * @version 1.0.0
	 */
	@Override
	public List<PaperArchive> findByUnitCode(String code) {
		if (code.length() == 2) {// 校长
			return findAll();
		} else if (code.length() == 4) {// 院长
			return getResultList("from PaperArchive s where s.collegeCode = :code",
					wrap("code"), wrap(code));
		} else if (code.length() == 6) {//老师
			return getResultList("from PaperArchive s where s.majorCode = :code",
					wrap("code"), wrap(code));
		}
		else
			return null;
	}

}